package com.atguigu.day09;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.AggregateFunction;
import org.apache.flink.table.functions.TableAggregateFunction;
import org.apache.flink.util.Collector;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.call;

public class Flink09_Fun_UDTAF {
    public static void main(String[] args) {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.从端口读取数据
        SingleOutputStreamOperator<WaterSensor> streamSource = env.socketTextStream("localhost", 9999)
                .map(new MapFunction<String, WaterSensor>() {
                    @Override
                    public WaterSensor map(String value) throws Exception {
                        String[] split = value.split(",");
                        return new WaterSensor(split[0], Long.parseLong(split[1]), Integer.parseInt(split[2]));
                    }
                });

        //3.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        //4.将流转为表
        Table table = tableEnv.fromDataStream(streamSource);

        //不注册直接使用
//        table
//                .groupBy($("id"))
//                .flatAggregate(call(MyUDTAF.class, $("vc")))
//                .select($("id"),$("f0"),$("f1"))
//                .execute()
//                .print();

//        table
//                .groupBy($("id"))
//                .flatAggregate(call(MyUDTAF.class, $("vc")).as("value", "rank"))
//                .select($("id"),$("value"),$("rank"))
//                .execute()
//                .print();
        //先注册再使用
        tableEnv.createTemporarySystemFunction("MyTop2", MyUDTAF.class);

        //API中使用
                table
                .groupBy($("id"))
                .flatAggregate(call("MyTop2", $("vc")).as("value", "rank"))
                .select($("id"),$("value"),$("rank"))
                .execute()
                .print();

    }

    //自定义一个类实现UDTAF（表聚合函数TableAggregateFunction）函数，根据VC求Top2
    public static class MyTopAcc{
        public Integer first;
        public Integer second;
    }
   public static class MyUDTAF extends TableAggregateFunction<Tuple2<Integer,Integer>,MyTopAcc>{

       /**
        * 初始化累加器
        * @return
        */
       @Override
       public MyTopAcc createAccumulator() {
           MyTopAcc myTopAcc = new MyTopAcc();
           myTopAcc.first = Integer.MIN_VALUE;
           myTopAcc.second = Integer.MIN_VALUE;
           return myTopAcc;
       }

       /**
        * 累加操作
        */
       public void accumulate(MyTopAcc acc, Integer value) {
           //当前值大于第一名
           if (value>acc.first) {
               acc.second = acc.first;
               acc.first = value;
               //如果不排第一的话看是否排第二
           }else if (value>acc.second) {
               acc.second = value;
           }

       }
       /**
        * 返回结果
        */
       public void emitValue(MyTopAcc acc, Collector<Tuple2<Integer, Integer>> out) {
           if (acc.first!=Integer.MIN_VALUE){
               out.collect(Tuple2.of(acc.first,1));
           }

           if (acc.second!=Integer.MIN_VALUE){
               out.collect(Tuple2.of(acc.second,2));
           }

       }
   }

}
